import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
} from '@nestjs/common';
import { ApiOperation, ApiProperty, ApiTags } from '@nestjs/swagger';

// 定义一个类，对post接口做约束
class CreatePostDto {
  @ApiProperty({ description: '帖子标题' })
  title: string;
  @ApiProperty({ description: '帖子内容' })
  content: string;
}

@Controller('posts')
@ApiTags('帖子')
export class PostsController {
  @Get()
  @ApiOperation({ summary: '显示帖子列表' })
  index() {
    return [
      { id: 1, title: '帖子111' },
      { id: 2, title: '帖子222' },
      { id: 3, title: '帖子333' },
    ];
  }

  @Post()
  @ApiOperation({ summary: '新增帖子' })
  create(@Body() body: CreatePostDto, @Query() query, @Param() param) {
    //@Body()参数装饰器
    //后面的body名字随便取的，意思是从@Body()里面取出数据给body
    return body;
  }

  @Get(':id')
  @ApiOperation({ summary: '帖子详情' })
  detail(@Param('id') id: string) {
    return {
      id: id,
      title: 'aaaaaa',
    };
  }

  @Put(':id/:type')
  @ApiOperation({ summary: '修改帖子' })
  update(
    @Param('id') id: string,
    @Param('type') type: string,
    @Body() body: CreatePostDto,
  ) {
    return {
      id: id,
      type: type,
      data: body,
    };
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除帖子' })
  delete(@Param('id') id: string) {
    return {
      id,
      msg: '删除成功',
    };
  }
}
